<?php

class CityOrder extends BaseModel
{
    public $res;
    public $expire;

    public function getSource()
    {
        return "city_order";
    }

    public function find_by_id_obj($id){
        return $this->findFirstById($id);
    }

    public function get_user_money($date)
    {
        $condition['conditions'] = "settlement_type = 0 and user_id in ({$date['user_id']}) and tk_earning_time >= '{$date['start_at']}' and tk_earning_time <='{$date['end_at']}'";
        $condition['columns']    = 'user_id,sum(money) as money';
        $condition['group']      = 'user_id';
        $res                     = $this->find($condition)->toarray();
        if ($res == []) {
            return [];
        }
        $orderId = $this->find([
            'conditions' => $condition['conditions'],
            //            'columns'    => 'id,order_id'
        ]);

        if (!empty($orderId->toarray())) {
            $orderIdArr = [];
            foreach ($orderId as $val) {
                $val->settlement_type = 1;
                $orderIdArr[]         = $val->order_id;
                if (!$val->save()) {
                    return false;
                }
            }
            $order      = new Orders();
            $orderIdStr = implode(',', $orderIdArr);
            $orderlist  = $order->find([
                'conditions' => "order_id in ($orderIdStr)"
            ]);
            if (!empty($orderlist)) {
                foreach ($orderlist as $value) {
                    $value->is_settlement = 1;
                    if (!$value->save()) {
                        return false;
                    }
                }
            }
        }
        return $res;
    }
}
